<template>
  <div>
    <van-cell-group inset>
      <van-field
        :value="editInfo.params"
        :name="editInfo.editName"
        :label="editInfo.editName"
        placeholder="请输入信息"
        :rules="[{ required: true, message: `请输入信息` }]"
        custom-style="{height:100px}"
        @change="onChange"
      >
        <template
          #input
          v-if="editInfo.keyWord !== null && editInfo.keyWord === 'avatarUrl'"
        >
          <div>
            <van-image
              class="img"
              width="40"
              height="40"
              fit="cover"
              :src="editInfo.params"
              alt=""
              @click="uploadAvatar"
            />
          </div>
        </template>
      </van-field>
    </van-cell-group>
    <div style="margin: 16px">
      <van-button round block type="primary" @click="onSubmit">
        提交
      </van-button>
    </div>
  </div>
  <van-notify id="van-notify" />
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue'
import useStore from '../../store'
import { updateUser } from '../../api/user/userApi'

import { onShow } from '@dcloudio/uni-app';
let editInfo = reactive({
  params: '编辑参数',
  editName: '编辑名称',
  keyWord: '编辑对象',
})

console.log(editInfo)

editInfo = useStore().user().editUser
console.log(useStore().user().editUser)
console.log(editInfo)
if (editInfo.editName === '性别') {
  editInfo.params = editInfo.params === 0 ? '男' : '女'
}
const onChange = (e) => {
  console.log(e.detail)
  editInfo.params = e.detail
}
const onSubmit = () => {
  if (editInfo.editName === '性别') {
    if (editInfo.params === '男') {
      editInfo.params = 0
    } else {
      editInfo.params = 1
    }
  }

  console.log(editInfo)
  const obj = {
    [editInfo.keyWord]: editInfo?.params,
  }
  console.log(obj)
  var pages = getCurrentPages() // 获取当前挂载的路由数组
  var prePage = pages[pages.length - 2] //获取 上一个页面
  updateUser(obj).then((res) => {
    useStore().user().judgeFlash(true)
    if (editInfo.editName === '性别') {
      if (editInfo.params === 0) {
        editInfo.params = '男'
      } else {
        editInfo.params = '女'
      }
    }
    //在这里需要做判断是如果修改是性别，修改成功后会有1.5s延时后才返回上一页，
    // 在这个时间内，数据会以原始的形式渲染，即男=》0，女=》1
    //所以需要特判，拒绝显示这个bug
    uni.showToast({
      title: '成功',
      success: () => {
        setTimeout(() => {
          uni.navigateBack({
            delta: 1,
            success: () => {
              console.log(prePage)
            },
          })
        }, 1500)
      },
    })
    console.log(res)
  })
}
const uploadAvatar =async () => {
  uni.chooseImage({
        count: 1, //默认9
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], //从相册选择
        success: function (res) {
            console.log(JSON.stringify(res.tempFilePaths));
            uni.uploadFile({
                url:'https://www.pickteams.cn/api/team/upload',
                filePath:res.tempFilePaths[0],
                name: 'file',
                header:{
                    "Authorization":uni.getStorageSync('Authorization'),
                    "content-type":"utf-8"
                },
                success: (uploadFileRes) => {
                    console.log(uploadFileRes.data);
                    const result=JSON.parse(uploadFileRes.data)
                    if(result.code===0){
                        uni.showToast({
                            title:'上传成功',
                            icon:'success',
                            success:()=>{
                              editInfo.params=result.data
                            } 
                            
                        })
                        useStore().user().updateAvartar(result.data)
                    }else{
                        uni.showToast({
                            title:'上传失败',
                            icon:'fail'


                        })
                    }
                }
            })
        }
    });

 
 


}

onMounted(() => {})
</script>
<style scoped>
.img {
  width: 40px;
  height: 40px;
}
/deep/.van-field__control {
  height: 50px !important;
  
}
/deep/.van-field__label{
  line-height: 50px !important;
}
</style>
